package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-06-23
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
@Api(tags = "课程小结")
@Slf4j
public class EduVideoController {
    //注入小节
    @Autowired
    private VodClient vodClient;




    @Autowired
    private EduVideoService eduVideoService;
    //添加小结
    @ApiModelProperty(value = "添加小结")
    @PostMapping("addVideo")
    public Result addVideo(@RequestBody EduVideo eduVideo){

        log.info("添加小结:"+ eduVideo);
        eduVideoService.save(eduVideo);
        return Result.ok();
    }

    //根据小结id查询章节
    @ApiModelProperty(value = "根据课程小结id查询章节")
    @GetMapping("getVideoInfo/{videoId}")
    public Result getVideoInfo(@PathVariable String videoId){
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return Result.ok().data("eduVideo",eduVideo);
    }
    //修改小结
    @ApiModelProperty(value = "修改小结")
    @PostMapping("updateVideo")
    public Result updateVideo(@RequestBody EduVideo eduVideo){
        log.info("修改小结:"+ eduVideo);
        boolean b = eduVideoService.updateById(eduVideo);
        if (b){
            return Result.ok();
        }
        return Result.error();
    }

    //删除小结，删除对应阿里云视频
    @ApiModelProperty(value = "删除小结")
    @DeleteMapping("deleteVideo/{id}")
    public Result deleteVideo(@PathVariable String id){
        //根据小结id获取视频id，调用方法实现视频删除
        EduVideo eduVideo = eduVideoService.getById(id);
        log.info("我是谁呀：" + eduVideo);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
//        根据小节id获取视频id，远程调用实现视频删除
            log.info("到这就算是执行呢");
            vodClient.removeAlyVideo(videoSourceId);
        }
        eduVideoService.removeById(id);
        return Result.ok();
    }











}